package entity;

public class board {
	private int width;
	private int[][] obstacles; // Use 2 dimensions array to represent the
								// obstacles.1st dimension represents the
								// index while the last one represents
								// the location of the obstacle
	private int obstacleCount; // used for insert obstacle
	private agent A, B;

	/*
	 * It seems this function has never been called.
	 */
	// public int getWidth() {
	// return width;
	// }

	public agent getA() {
		return A;
	}

	public agent getB() {
		return B;
	}

	/*
	 * constructors
	 */
	public board() {
		width = 0;
		obstacles = new int[0][2];
	}

	public board(int width, int numberOfObstacles) {
		this.width = width;
		obstacles = new int[numberOfObstacles][2];
	}

	/*
	 * Set an obstacle on the board return whether it succeed or not
	 */
	public boolean setObstacle(int x, int y) {
		if (!isAccessible(x, y)) {
			System.out.println("Obstacle Posttion (" + x + "," + y
					+ ") is illegal!");
			return false;
		}
		if (obstacleCount < obstacles.length) {
			obstacles[obstacleCount][0] = x;
			obstacles[obstacleCount][1] = y;
			obstacleCount++;
			return true;
		} else {
			return false;
		}
	}

	/*
	 * Set the agent A on the board. If A exists, the old one will be replaced.
	 */
	public boolean putAgentA(int x, int y, int[] moveSostTable) {
		if (!isAccessible(x, y)) {
			System.out.println("Agent Posttion (" + x + "," + y
					+ ") is illegal!");
			return false;
		}
		A = new agent(this, x, y, moveSostTable);
		return true;
	}

	/*
	 * Set the location of agent A
	 */
	public boolean setAgentA(int x, int y) {
		if (!isAccessible(x, y)) {
			System.out.println("Agent Posttion (" + x + "," + y
					+ ") is illegal!");
			return false;
		}
		A.setX(x);
		A.setY(y);
		return true;
	}

	/*
	 * Set the agent B on the board. If B exists, the old one will be replaced.
	 */
	public boolean putAgentB(int x, int y, int[] moveCostTable) {
		if (!isAccessible(x, y)) {
			System.out.println("Agent Posttion (" + x + "," + y
					+ ") is illegal!");
			return false;
		}
		B = new agent(this, x, y, moveCostTable);
		return true;
	}

	/*
	 * Set the location of agent B
	 */
	public boolean setAgentB(int x, int y) {
		if (!isAccessible(x, y)) {
			System.out.println("Agent Posttion (" + x + "," + y
					+ ") is illegal!");
			return false;
		}
		B.setX(x);
		B.setY(y);
		return true;
	}

	/*
	 * Set the board state(the location of A and B) by the state object s
	 */
	public void setSate(state s) {
		setAgentA(s.getAx(), s.getAy());
		setAgentB(s.getBx(), s.getBy());
	}

	/*
	 * Test that if there is any obstacle at the position(x,y) or if the x or y
	 * is out of the board range.
	 * If both are not, the position is accessible,or it isn't. 
	 * Return the accessibility.
	 */
	public boolean isAccessible(int x, int y) {
		if (x > width || x < 1 || y > width || y < 1)
			return false;
		for (int i = 0; i < obstacles.length; i++) {
			if (obstacles[i][0] == x)
				if (obstacles[i][1] == y)
					return false;
		}
		return true;
	}

	/*
	 * Generate a state object according the current state of the board(the
	 * location of A and B). Return the state object
	 */
	public state getCurrentState() {
		return new state(A.getX(), A.getY(), B.getX(), B.getY());
	}

}
